﻿using System.Threading;
using System.Windows.Forms;
using WORD = Microsoft.Office.Interop.Word;
using System.Web;
using System;
using System.IO;
using System.Text;
using System.Configuration;
using System.Collections.Generic;
namespace QuanLyHocPhi.BLL
{
    public class WordUltities : IDisposable
    {
        #region Object Const

        object oMissing = System.Reflection.Missing.Value;
        object oVisible = false;
        object oReadOnly = false;
        object oShowConvention = false;
        object oOpenandRepair = true;
        object oTrue = true;
        object oFalse = false;

        #endregion

        #region Fields
        private WORD._Application _wApp = null;
        public WORD._Application WordApplication
        {
            //
            get
            {
                return _wApp ?? (_wApp = new WORD.Application());
            }
        }
        private WORD.Document _wDoc = null;
        public string FileName { get; set; }
        #endregion

        #region Constructor
        public WordUltities()
        {
        }
        public WordUltities(string fileName)
        {
            FileName = fileName;
        }
        #endregion

        #region public function

        public void OpenDocument()
        {
            object filen = FileName;
            _wDoc = WordApplication.Documents.Open(ref filen, ref oMissing, ref oMissing, ref oMissing,
                 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            WordApplication.Visible = false;
        }

        public bool ReplaceText(string findText, string replaceText)
        {
            // lấy tham số
            object obh = WORD.WdDefaultTableBehavior.wdWord9TableBehavior;
            object oaf = WORD.WdAutoFitBehavior.wdAutoFitFixed;
            object omove = WORD.WdMovementType.wdMove;
            object ounitS = WORD.WdUnits.wdScreen;
            object otableBh = WORD.WdDefaultTableBehavior.wdWord9TableBehavior;
            object otableAf = WORD.WdAutoFitBehavior.wdAutoFitFixed;
            object ounitCl = WORD.WdUnits.wdCell;
            object ounitL = WORD.WdUnits.wdLine;
            object ounitC = WORD.WdUnits.wdCharacter;
            object ounitSep = WORD.WdTableFieldSeparator.wdSeparateByTabs;
            object ont = true;
            object ofalse = false;
            object ocount = 3;
            object oreplace;
            oreplace = WORD.WdReplace.wdReplaceAll;
            object ofindtext = findText;
            object orpltext = replaceText;
            WordApplication.Selection.Find.Text = findText;
            WordApplication.Selection.Find.Replacement.Text = replaceText;
            WordApplication.Selection.Find.Forward = true;
            WordApplication.Selection.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
            WordApplication.Selection.Find.MatchCase = false;
            return WordApplication.Selection.Find.Execute(ref ofindtext, ref oTrue, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref orpltext,
                ref oreplace, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
        }

        public void PrintDocument()
        {
            int doi = 10000;
            bool visible = false;
            bool autoClose = true;
            try
            {
                try
                {
                    if (!File.Exists("cauhinh.txt"))
                    {
                        StreamWriter writer = new StreamWriter("cauhinh.txt");
                        writer.Write("10000;false;true");
                        writer.Close();
                    }
                    System.IO.StreamReader read = new StreamReader("cauhinh.txt");
                    string dong = read.ReadLine();
                    read.Close();
                    string[] temp = dong.Split(new string[] { ";" }, StringSplitOptions.None);
                    int r = 0;
                    if (temp.Length == 3)
                    {
                        if (int.TryParse(temp[0], out r))
                        {
                            doi = int.Parse(temp[0]);
                            if (temp[1] == "false")
                                visible = false;
                            else
                                visible = true;
                            if (visible)
                                if (temp[2] == "true")
                                    autoClose = true;
                                else
                                    autoClose = false;
                        }
                        else
                        {
                            StreamWriter writer = new StreamWriter("cauhinh.txt");
                            writer.Write("10000;false;true");
                            writer.Close();
                        }
                    }
                }
                catch (Exception)
                {
                }
                if (visible)
                    WordApplication.Visible = true;
                else
                    WordApplication.Visible = false;
                WordApplication.PrintOut();
                Thread.Sleep(doi);
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi in phiếu thu. Chi tiết lỗi: " + ex.ToString());
            }
            finally
            {
                try
                {
                    if (autoClose)
                    {
                        EndWork();
                        WordApplication.Quit();
                    }
                }
                catch
                { }
            }
        }

        WORD.Document CloseDocument(bool saveChanges)
        {
            object oSave = saveChanges;
            WORD._Document doc = _wDoc;
            doc.Close(ref oSave, ref oMissing, ref oMissing);
            return _wDoc;
        }

        bool EndWork()
        {
            _wApp.Quit(ref oFalse, ref oMissing, ref oMissing);
            WordApplication.Quit(ref oFalse, ref oMissing, ref oMissing);
            _wApp = null;
            return true;
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            //this.Dispose();
        }

        #endregion
    }
}
